﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using DataRecv.tools;
using System.Data;
using DataRecv.enums;
using DataRecv.db;
using DataRecv.models.road;

namespace DataRecv.models.device
{
    /// <summary>
    /// 设备参数
    /// </summary>
    public class DeviceParam
    {
        /// <summary>
        /// 表名称
        /// </summary>
        private const string c_TableName = "DeviceParam_t";
        /// <summary>
        /// 字段集合
        /// </summary>
        private const string c_Columns = "ip,roadid,sbbh,type,workmode,direction,speedlimit,deviceusername,devicepassword,mutiroad,channel1roadid,channel2roadid,channel3roadid,channel4roadid,channel5roadid,channel6roadid,channel7roadid,channel8roadid,djsnapbyplate,platetypesnapby,djsnapbytime,time1_start,time1_end,time2_start,time2_end,time3_start,time3_end,time4_start,time4_end,staticpicenable,staticpicindex,staticpic,bz,picwidth,picheight";
        /// <summary>
        /// 字段数量
        /// </summary>
        private const int c_ColumnsCount = 35;
        /// <summary>
        /// 设备ip
        /// </summary>
        public string ip;
        /// <summary>
        /// 所属路口编号
        /// </summary>
        public string roadid;
        /// <summary>
        /// 设备编号
        /// </summary>
        public string sbbh;
        /// <summary>
        /// 设备类型
        /// </summary>
        public Enum_DeviceType type;
        /// <summary>
        /// 设备工作模式
        /// </summary>
        public Enum_CaptureSystem workmode;
        /// <summary>
        /// 设备方向
        /// </summary>
        public Enum_Direction direction;
        /// <summary>
        /// 限速
        /// </summary>
        public string speedlimit;
        /// <summary>
        /// 用户名
        /// </summary>
        public string deviceusername;
        /// <summary>
        /// 密码
        /// </summary>
        public string devicepassword;
        /// <summary>
        /// 下挂多路口
        /// </summary>
        public bool mutiroad;
        /// <summary>
        /// 通道1对应的roadid
        /// </summary>
        public string channel1roadid;
        /// <summary>
        /// 通道2对应的roadid
        /// </summary>
        public string channel2roadid;
        /// <summary>
        /// 通道3对应的roadid
        /// </summary>
        public string channel3roadid;
        /// <summary>
        /// 通道4对应的roadid
        /// </summary>
        public string channel4roadid;
        /// <summary>
        /// 通道5对应的roadid
        /// </summary>
        public string channel5roadid;
        /// <summary>
        /// 通道6对应的roadid
        /// </summary>
        public string channel6roadid;
        /// <summary>
        /// 通道7对应的roadid
        /// </summary>
        public string channel7roadid;
        /// <summary>
        /// 通道8对应的roadid
        /// </summary>
        public string channel8roadid;
        /// <summary>
        /// 电警按车牌抓拍
        /// </summary>
        public bool djsnapbyplate;
        /// <summary>
        /// 电警按车牌抓拍类型
        /// </summary>
        public Enum_PlateTypeSnapBy platetypesnapby;
        /// <summary>
        /// 电警按时段抓拍
        /// </summary>
        public bool djsnapbytime;
        /// <summary>
        /// 时段1开始时间
        /// </summary>
        public string time1_start;
        /// <summary>
        /// 时段1结束时间
        /// </summary>
        public string time1_end;

        /// <summary>
        /// 时段2开始时间
        /// </summary>
        public string time2_start;
        /// <summary>
        /// 时段2结束时间
        /// </summary>
        public string time2_end;

        /// <summary>
        /// 时段3开始时间
        /// </summary>
        public string time3_start;
        /// <summary>
        /// 时段3结束时间
        /// </summary>
        public string time3_end;

        /// <summary>
        /// 时段4开始时间
        /// </summary>
        public string time4_start;
        /// <summary>
        /// 时段4结束时间
        /// </summary>
        public string time4_end;
        /// <summary>
        /// 启用静态图
        /// </summary>
        public bool staticpicenable;
        /// <summary>
        /// 静态图序号
        /// </summary>
        public int staticpicindex;
        /// <summary>
        /// 静态图
        /// </summary>
        public string staticpic;

        /// <summary>
        /// 备注
        /// </summary>
        public string bz;

        /// <summary>
        /// 图片宽度
        /// </summary>
        public int picwidth;

        //图片高度
        public int picheight;

        /// <summary>
        /// 设备能图片合成
        /// </summary>
        public bool CanJoin
        {
            get
            {
                bool ans = false;
                switch (type)
                {
                    case Enum_DeviceType.DH: ans = true;
                        break;
                    case Enum_DeviceType.HK5012: ans = true;
                        break;
                    case Enum_DeviceType.HK9121: ans = true;
                        break;
                    case Enum_DeviceType.HK9151: ans = true;
                        break;
                    case Enum_DeviceType.HK9361: ans = true;
                        break;
                    case Enum_DeviceType.HK9371: ans = true;
                        break;
                    case Enum_DeviceType.HK986: ans = false;
                        break;
                    case Enum_DeviceType.HV: ans = false;
                        break;
                    case Enum_DeviceType.MaxFord: ans = false;
                        break;
                    default: ans = false;
                        break;
                }
                return ans;
            }
        }

        /// <summary>
        /// 设备能图片压缩
        /// </summary>
        public bool CanCompress
        {
            get
            {
                bool ans = false;
                switch (type)
                {
                    case Enum_DeviceType.DH: ans = true;
                        break;
                    case Enum_DeviceType.HK5012: ans = true;
                        break;
                    case Enum_DeviceType.HK9121: ans = true;
                        break;
                    case Enum_DeviceType.HK9151: ans = true;
                        break;
                    case Enum_DeviceType.HK9361: ans = true;
                        break;
                    case Enum_DeviceType.HK9371: ans = true;
                        break;
                    case Enum_DeviceType.HK986: ans = false;
                        break;
                    case Enum_DeviceType.HV: ans = false;
                        break;
                    case Enum_DeviceType.MaxFord: ans = false;
                        break;
                    default: ans = false;
                        break;
                }
                return ans;
            }
        }

        /// <summary>
        /// 设备能字符叠加
        /// </summary>
        public bool CanPrint
        {
            get
            {
                bool ans = false;
                switch (type)
                {
                    case Enum_DeviceType.DH: ans = true;
                        break;
                    case Enum_DeviceType.HK5012: ans = true;
                        break;
                    case Enum_DeviceType.HK9121: ans = true;
                        break;
                    case Enum_DeviceType.HK9151: ans = true;
                        break;
                    case Enum_DeviceType.HK9361: ans = true;
                        break;
                    case Enum_DeviceType.HK9371: ans = true;
                        break;
                    case Enum_DeviceType.HK986: ans = true;
                        break;
                    case Enum_DeviceType.HV: ans = false;
                        break;
                    case Enum_DeviceType.MaxFord: ans = false;
                        break;
                    default: ans = false;
                        break;
                }
                return ans;
            }
        }

        /// <summary>
        /// 空构造函数
        /// </summary>
        public DeviceParam()
        {
            ip = "";
            roadid = "";
            sbbh = "";
            type = Enum_DeviceType.HK9151;
            workmode = Enum_CaptureSystem.NONE;
            direction = Enum_Direction.由东向西;
            speedlimit = "";
            deviceusername = "";
            devicepassword = "";
            mutiroad = false;
            channel1roadid = "";
            channel2roadid = "";
            channel3roadid = "";
            channel4roadid = "";
            channel5roadid = "";
            channel6roadid = "";
            channel7roadid = "";
            channel8roadid = "";
            djsnapbyplate = false;
            platetypesnapby = Enum_PlateTypeSnapBy.抓拍全部车牌;
            djsnapbytime = false;
            time1_start = "00:00";
            time1_end = "00:00";
            time2_start = "00:00";
            time2_end = "00:00";
            time3_start = "00:00";
            time3_end = "00:00";
            time4_start = "00:00";
            time4_end = "00:00";
            staticpicenable = false;
            staticpicindex = 1;
            staticpic = "";
            bz = "";
            picwidth = 2448;
            picheight = 2080;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <returns>所有设备参数</returns>
        public static List<DeviceParam> GetModels()
        {
            List<DeviceParam> ans = null;
            DataTable dt = null;
            try
            {
                dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "1=1");
                if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0))
                {
                    ans = new List<DeviceParam>();
                    foreach (DataRow row in dt.Rows)
                    {
                        DeviceParam p = new DeviceParam();
                        p.ip = row["ip"].ToString();
                        p.roadid = row["roadid"].ToString();
                        p.sbbh = row["sbbh"].ToString();
                        p.type = (Enum_DeviceType)Enum.Parse(typeof(Enum_DeviceType), row["type"].ToString());
                        p.workmode = (Enum_CaptureSystem)Enum.Parse(typeof(Enum_CaptureSystem), row["workmode"].ToString());
                        p.direction = (Enum_Direction)Enum.Parse(typeof(Enum_Direction), row["direction"].ToString());
                        p.speedlimit = row["speedlimit"].ToString();
                        p.deviceusername = row["deviceusername"].ToString();
                        p.devicepassword = row["devicepassword"].ToString();
                        p.mutiroad = bool.Parse(row["mutiroad"].ToString());
                        p.channel1roadid = row["channel1roadid"].ToString();
                        p.channel2roadid = row["channel2roadid"].ToString();
                        p.channel3roadid = row["channel3roadid"].ToString();
                        p.channel4roadid = row["channel4roadid"].ToString();
                        p.channel5roadid = row["channel5roadid"].ToString();
                        p.channel6roadid = row["channel6roadid"].ToString();
                        p.channel7roadid = row["channel7roadid"].ToString();
                        p.channel8roadid = row["channel8roadid"].ToString();
                        p.djsnapbyplate = bool.Parse(row["djsnapbyplate"].ToString());
                        p.platetypesnapby = (Enum_PlateTypeSnapBy)Enum.Parse(typeof(Enum_PlateTypeSnapBy), row["platetypesnapby"].ToString());
                        p.djsnapbytime = bool.Parse(row["djsnapbytime"].ToString());
                        p.time1_start = row["time1_start"].ToString();
                        p.time1_end = row["time1_end"].ToString();
                        p.time2_start = row["time2_start"].ToString();
                        p.time2_end = row["time2_end"].ToString();
                        p.time3_start = row["time3_start"].ToString();
                        p.time3_end = row["time3_end"].ToString();
                        p.time4_start = row["time4_start"].ToString();
                        p.time4_end = row["time4_end"].ToString();
                        p.staticpicenable = bool.Parse(row["staticpicenable"].ToString());
                        p.staticpicindex = int.Parse(row["staticpicindex"].ToString());
                        p.staticpic = row["staticpic"].ToString();
                        p.bz = row["bz"].ToString();
                        p.picwidth = int.Parse(row["picwidth"].ToString());
                        p.picheight = int.Parse(row["picheight"].ToString());
                        ans.Add(p);
                    }
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("DeviceParam.GetModels()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="deviceid">设备编号</param>
        /// <returns>设备参数</returns>
        public static DeviceParam GetModel(string ip)
        {
            DeviceParam ans = null;
            DataTable dt = null;
            try
            {
                if ((ip != null) && (ip != ""))
                {
                    dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "ip='" + ip + "'");
                    if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count == 1))
                    {
                        ans = new DeviceParam();
                        ans.ip = dt.Rows[0]["ip"].ToString();
                        ans.roadid = dt.Rows[0]["roadid"].ToString();
                        ans.sbbh = dt.Rows[0]["sbbh"].ToString();
                        ans.type = (Enum_DeviceType)Enum.Parse(typeof(Enum_DeviceType), dt.Rows[0]["type"].ToString());
                        ans.workmode = (Enum_CaptureSystem)Enum.Parse(typeof(Enum_CaptureSystem), dt.Rows[0]["workmode"].ToString());
                        ans.direction = (Enum_Direction)Enum.Parse(typeof(Enum_Direction), dt.Rows[0]["direction"].ToString());
                        ans.speedlimit = dt.Rows[0]["speedlimit"].ToString();
                        ans.deviceusername = dt.Rows[0]["deviceusername"].ToString();
                        ans.devicepassword = dt.Rows[0]["devicepassword"].ToString();
                        ans.mutiroad = bool.Parse(dt.Rows[0]["mutiroad"].ToString());
                        ans.channel1roadid = dt.Rows[0]["channel1roadid"].ToString();
                        ans.channel2roadid = dt.Rows[0]["channel2roadid"].ToString();
                        ans.channel3roadid = dt.Rows[0]["channel3roadid"].ToString();
                        ans.channel4roadid = dt.Rows[0]["channel4roadid"].ToString();
                        ans.channel5roadid = dt.Rows[0]["channel5roadid"].ToString();
                        ans.channel6roadid = dt.Rows[0]["channel6roadid"].ToString();
                        ans.channel7roadid = dt.Rows[0]["channel7roadid"].ToString();
                        ans.channel8roadid = dt.Rows[0]["channel8roadid"].ToString();
                        ans.djsnapbyplate = bool.Parse(dt.Rows[0]["djsnapbyplate"].ToString());
                        ans.platetypesnapby = (Enum_PlateTypeSnapBy)Enum.Parse(typeof(Enum_PlateTypeSnapBy), dt.Rows[0]["platetypesnapby"].ToString());
                        ans.djsnapbytime = bool.Parse(dt.Rows[0]["djsnapbytime"].ToString());
                        ans.time1_start = dt.Rows[0]["time1_start"].ToString();
                        ans.time1_end = dt.Rows[0]["time1_end"].ToString();
                        ans.time2_start = dt.Rows[0]["time2_start"].ToString();
                        ans.time2_end = dt.Rows[0]["time2_end"].ToString();
                        ans.time3_start = dt.Rows[0]["time3_start"].ToString();
                        ans.time3_end = dt.Rows[0]["time3_end"].ToString();
                        ans.time4_start = dt.Rows[0]["time4_start"].ToString();
                        ans.time4_end = dt.Rows[0]["time4_end"].ToString();
                        ans.staticpicenable = bool.Parse(dt.Rows[0]["staticpicenable"].ToString());
                        ans.staticpicindex = int.Parse(dt.Rows[0]["staticpicindex"].ToString());
                        ans.staticpic = dt.Rows[0]["staticpic"].ToString();
                        ans.bz = dt.Rows[0]["bz"].ToString();
                        ans.picwidth = int.Parse(dt.Rows[0]["picwidth"].ToString());
                        ans.picheight = int.Parse(dt.Rows[0]["picheight"].ToString());
                    }
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("DeviceParam.GetModel()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="param">设备参数</param>
        /// <returns>结果</returns>
        public static bool AddModel(DeviceParam param)
        {
            bool ans = false;
            try
            {
                string[] values = new string[c_ColumnsCount];
                values[0] = param.ip;
                values[1] = param.roadid;
                values[2] = param.sbbh;
                values[3] = param.type.ToString();
                values[4] = param.workmode.ToString();
                values[5] = param.direction.ToString();
                values[6] = param.speedlimit;
                values[7] = param.deviceusername;
                values[8] = param.devicepassword;
                values[9] = param.mutiroad.ToString();
                values[10] = param.channel1roadid;
                values[11] = param.channel2roadid;
                values[12] = param.channel3roadid;
                values[13] = param.channel4roadid;
                values[14] = param.channel5roadid;
                values[15] = param.channel6roadid;
                values[16] = param.channel7roadid;
                values[17] = param.channel8roadid;
                values[18] = param.djsnapbyplate.ToString();
                values[19] = param.platetypesnapby.ToString();
                values[20] = param.djsnapbytime.ToString();
                values[21] = param.time1_start;
                values[22] = param.time1_end;
                values[23] = param.time2_start;
                values[24] = param.time2_end;
                values[25] = param.time3_start;
                values[26] = param.time3_end;
                values[27] = param.time4_start;
                values[28] = param.time4_end;
                values[29] = param.staticpicenable.ToString();
                values[30] = param.staticpicindex.ToString();
                values[31] = param.staticpic;
                values[32] = param.bz;
                values[33] = param.picwidth.ToString();
                values[34] = param.picheight.ToString();
                ans = DBHelper.GetInstance().Add(c_TableName, c_Columns, values);
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("DeviceParam.AddModel()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="param">设备参数</param>
        /// <returns>结果</returns>
        public static bool EditModel(DeviceParam param)
        {
            bool ans = false;
            try
            {
                string[] values = new string[c_ColumnsCount];
                values[0] = param.ip;
                values[1] = param.roadid;
                values[2] = param.sbbh;
                values[3] = param.type.ToString();
                values[4] = param.workmode.ToString();
                values[5] = param.direction.ToString();
                values[6] = param.speedlimit;
                values[7] = param.deviceusername;
                values[8] = param.devicepassword;
                values[9] = param.mutiroad.ToString();
                values[10] = param.channel1roadid;
                values[11] = param.channel2roadid;
                values[12] = param.channel3roadid;
                values[13] = param.channel4roadid;
                values[14] = param.channel5roadid;
                values[15] = param.channel6roadid;
                values[16] = param.channel7roadid;
                values[17] = param.channel8roadid;
                values[18] = param.djsnapbyplate.ToString();
                values[19] = param.platetypesnapby.ToString();
                values[20] = param.djsnapbytime.ToString();
                values[21] = param.time1_start;
                values[22] = param.time1_end;
                values[23] = param.time2_start;
                values[24] = param.time2_end;
                values[25] = param.time3_start;
                values[26] = param.time3_end;
                values[27] = param.time4_start;
                values[28] = param.time4_end;
                values[29] = param.staticpicenable.ToString();
                values[30] = param.staticpicindex.ToString();
                values[31] = param.staticpic;
                values[32] = param.bz;
                values[33] = param.picwidth.ToString();
                values[34] = param.picheight.ToString();
                ans = DBHelper.GetInstance().Edit(c_TableName, c_Columns, values, "ip='" + param.ip + "'");
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("DeviceParam.EditModel()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="deviceid">设备编号</param>
        /// <returns>结果</returns>
        public static bool DeleteModel(string ip)
        {
            bool ans = false;
            try
            {
                if ((ip != null) && (ip != ""))
                {
                    ans = DBHelper.GetInstance().Delete(c_TableName, "ip='" + ip + "'");
                }
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("DeviceParam.DeleteModel()" + er.Message);
            }
            return ans;
        }
    }
}
